﻿using FastCloud.Base.Config;
using FastCloud.Base.DB;
using FastCloud.Base.Desgin;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.ORG.DB
{
    public class DB_User : BaseDBML<Frame_User>
    {
        [AutoWired]
        private DB_OU @DB_OU;
        [AutoWired]
        private DB_UserExtend @DB_UserExtend;
        [AutoWired]
        private DB_Config @DB_Config;
        [AutoWired]
        private DB_Login @DB_Login;
        public override void Insert(Frame_User Entity)
        {
            Frame_OU OU = DB_OU.SelectOne(Entity.OUGuid);
            if (string.IsNullOrEmpty(Entity.UserGuid))
            {
                Entity.UserGuid = GenerateUtil.GenerateRowGuid();
            }
            Entity.Password = EncryptUtil.MD5Encrypt(Entity.LoginID + "|" + Entity.Password);
            Entity.AddDate = DateTime.Now;
            //取所属部门的TenantID
            Entity.TenantID = OU.TenantID;
            Entity.UpdateDate = DateTime.Now;
            base.Insert(Entity);
            //同步到Frame_Login
            Frame_Login Login = new Frame_Login();
            Login.LoginID = Entity.LoginID;
            Login.Password = Entity.Password;
            Login.UserGuid = Entity.UserGuid;
            Login.UserName = Entity.DisplayName;
            Login.Enable = Entity.Enable;
            Login.SyncFrom = (int)Base.Enum.ORG.SourceFrom.组织架构人员;
            Login.TenantID = Entity.TenantID;
            @DB_Login.Insert(Login);
            //计算部门下人员
            Add2Parameter("OUGuid", OU.OUGuid);
            OU.SubUserNum  = SelectCount(string.Format("OUGuid=@OUGuid"));
            @DB_OU.UpdateByID(OU);
        }

        public override void UpdateByID(Frame_User Entity)
        {
            Frame_User DBUser = SelectOne(Entity.UserGuid);
            Entity.UpdateDate = DateTime.Now;
            base.UpdateByID(Entity);
            if (DBUser.OUGuid != Entity.OUGuid)
            {
                Frame_OU DBOU = DB_OU.SelectOne(DBUser.OUGuid);
                Frame_OU NowOU = DB_OU.SelectOne(Entity.OUGuid);
                //计算部门下人员
                Add2Parameter("OUGuid", DBOU.OUGuid);
                DBOU.SubUserNum = SelectCount(string.Format("OUGuid=@OUGuid"));
                DB_OU.UpdateByID(DBOU);
                Add2Parameter("OUGuid", NowOU.OUGuid);
                NowOU.SubUserNum = SelectCount(string.Format("OUGuid=@OUGuid"));
                DB_OU.UpdateByID(NowOU);
            }
            //同步更新Frame_Login
            Frame_Login Login = new Frame_Login();
            Login.UserGuid = Entity.UserGuid;
            Login.UserGuid = Entity.DisplayName;
            Login.Enable = Entity.Enable;
            @DB_Login.UpdateByID(Login);
        }

        public override void DeleteByID(string UnitGuid)
        {
            Frame_User DBUser = SelectOne(UnitGuid);
            Frame_OU OU = DB_OU.SelectOne(DBUser.OUGuid);
            base.DeleteByID(UnitGuid);
            DB_UserExtend.DeleteByID(UnitGuid);
            Add2Parameter("OUGuid", UnitGuid);
            OU.SubUserNum = SelectCount(string.Format("OUGuid=@OUGuid")); ;
            DB_OU.UpdateByID(OU);
            //同步删除Frame_Login
            @DB_Login.DeleteByID(UnitGuid);
        }

        public void InitPWD(string UnitGuid)
        {
            Frame_User Entity = SelectOne(UnitGuid);
            Entity.Password = EncryptUtil.MD5Encrypt(Entity.LoginID + "|" + DB_Config.SelectConfigByName("DefaultPWD").ConfigValue);
            base.UpdateByID(Entity);
        }

    }
}
